
package com.dog.basics.service.wishwash.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.basics.bean.dto.api.wishwash.ApiDogHxDevNodeInfoDto;
import com.dog.basics.bean.model.wishwash.DogHxDev;
import com.dog.basics.bean.model.wishwash.DogHxOrderItme;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.wishwash.DogHxDevMapper;
import com.dog.basics.dao.wishwash.DogHxOrderItmeMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.bean.dto.admin.wishwash.AdminDogHxDevNodeAddDto;
import com.dog.basics.bean.dto.admin.wishwash.AdminDogHxDevNodeEditDto;
import com.dog.basics.bean.dto.admin.wishwash.AdminDogHxDevNodeFromDto;
import com.dog.basics.bean.vo.admin.wishwash.AdminDogHxDevNodeVo;
import com.dog.basics.bean.vo.admin.wishwash.AdminDogHxDevNodeInfoVo;
import com.dog.basics.bean.dto.api.wishwash.ApiDogHxDevNodeFromDto;
import com.dog.basics.bean.vo.api.wishwash.ApiDogHxDevNodeVo;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.model.wishwash.DogHxDevNode;
import com.dog.basics.dao.wishwash.DogHxDevNodeMapper;
import com.dog.basics.service.wishwash.DogHxDevNodeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 设备节点信息
 *
 * @author cy code generator
 * @date 2021-07-09 17:40:26
 */
@Service
@AllArgsConstructor
public class DogHxDevNodeServiceImpl extends ServiceImpl<DogHxDevNodeMapper, DogHxDevNode> implements DogHxDevNodeService {

    private final DogHxOrderItmeMapper orderItmeMapper;

    private final DogHxDevMapper hxDevMapper;

    /**
     * @Description: 后台管理分页
     * @Param: status
     * @return: IPage
     * @author cy code generator
     * @date 2021-07-09 17:40:26
     */
    @Override
    @SuppressWarnings("all")
    public IPage<AdminDogHxDevNodeVo> getAdminPageList(PageParam page, AdminDogHxDevNodeFromDto obj) {
        // 后台管理 - 分页查询数据信息
        this.page(page, Wrappers.<DogHxDevNode>lambdaQuery()
                .eq(DogHxDevNode::getDevId, obj.getDevId())
                .eq(ObjectUtil.isNotNull(obj.getNodeType()), DogHxDevNode::getNodeType, obj.getNodeType())
                .eq(ObjectUtil.isNotNull(obj.getNodeDevSize()), DogHxDevNode::getNodeDevSize, obj.getNodeDevSize())
                .between(ObjectUtil.isNotNull(obj.getStartTime()) && ObjectUtil.isNotNull(obj.getEndTime()), DogHxDevNode::getCreateTime, obj.getStartTime(), obj.getEndTime())
                .orderByAsc(DogHxDevNode::getNodeNo));
        // 格式化数据信息
        List<DogHxDevNode> records = page.getRecords();
        page.setRecords(records.stream().map(e -> {
            // 数据信息转换
            AdminDogHxDevNodeVo objVo = BeanUtil.toBean(e, AdminDogHxDevNodeVo.class);
            // 统计总共收入
            BigDecimal byDevNode = orderItmeMapper.getIncomeSumByDevNodeId(e.getId(), null, null);
            objVo.setTotalRevenue(byDevNode);
            return objVo;
        }).collect(Collectors.toList()));
        // 数据返回
        return page;
    }

    /**
     * @Description: 分页App
     * @Param: status
     * @return: IPage
     * @author cy code generator
     * @date 2021-07-09 17:40:26
     */
    @Override
    @SuppressWarnings("all")
    public IPage<ApiDogHxDevNodeVo> getApiPageList(PageParam page, ApiDogHxDevNodeFromDto obj) {
        // API - 分页查询数据信息
        this.page(page, Wrappers.<DogHxDevNode>lambdaQuery()
                .eq(DogHxDevNode::getDevId, obj.getDevId())
                .eq(ObjectUtil.isNotNull(obj.getNodeType()), DogHxDevNode::getNodeType, obj.getNodeType())
                .eq(ObjectUtil.isNotNull(obj.getNodeDevSize()), DogHxDevNode::getNodeDevSize, obj.getNodeDevSize())
                .between(ObjectUtil.isNotNull(obj.getStartTime()) && ObjectUtil.isNotNull(obj.getEndTime()), DogHxDevNode::getCreateTime, obj.getStartTime(), obj.getEndTime())
                .last("ORDER BY node_code+0 ASC"));
        // 格式化数据信息
        List<DogHxDevNode> records = page.getRecords();
        page.setRecords(records.stream().map(e -> {
            // 数据信息转换
            ApiDogHxDevNodeVo apiObjVo = BeanUtil.toBean(e, ApiDogHxDevNodeVo.class);
            // 下面自定义格式化返回数据
            return apiObjVo;
        }).collect(Collectors.toList()));
        return page;
    }

    /**
     * @Description: 后台管理查询详情
     * @Param: obj
     * @return: Boolean
     * @author cy code generator
     * @date 2021-07-09 17:40:26
     */
    @Override
    public AdminDogHxDevNodeInfoVo getAdminObjXq(Long id) {
        // 后台管理 - 查询数据信息是否存在
        DogHxDevNode obj = this.getById(id);
        // 判断数据是否存在
        Validator.validateNotNull(obj, "common.noData");
        // 数据信息格式化 - 返回
        AdminDogHxDevNodeInfoVo objVo = BeanUtil.toBean(obj, AdminDogHxDevNodeInfoVo.class);
        return objVo;
    }

    /**
     * @Description: 后台管理增加
     * @Param: obj
     * @return: Boolean
     * @author cy code generator
     * @date 2021-07-09 17:40:26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveAdminObj(AdminDogHxDevNodeAddDto obj) {
        // 判断设备 - 是否存在
        DogHxDev hxDev = hxDevMapper.selectById(obj.getDevId());
        Validator.validateNotNull(hxDev, "common.noData");
        // 后台管理 - 数据信息转换
        DogHxDevNode objAdd = BeanUtil.toBean(obj, DogHxDevNode.class);
        // 判断水温信息
        if (Validator.equal(obj.getNodeType(), 0)) {
            // 洗衣机
            // 判断是否为空
            Validator.validateTrue(CollUtil.isNotEmpty(obj.getNodeTemperatureOptions()), "dev.node.data.noNull");
            // 判断如果大于三个提示
            Validator.validateTrue(CollUtil.size(obj.getNodeTemperatureOptions()) <= 3, "dev.node.data.max");
            // 去重
            List<Integer> distinct = CollUtil.distinct(obj.getNodeTemperatureOptions());
            // 判断数据
            for (int i = distinct.size() - 1; i >= 0; i--) {
                // 判断是否小于 0 大于3
                Validator.validateFalse(distinct.get(i) < 0, "dev.node.data.error");
                Validator.validateFalse(distinct.get(i) > 3, "dev.node.data.error");
            }
            objAdd.setNodeTemperatureOption(JSONUtil.toJsonStr(distinct));
        } else {
            // 烘干机
            objAdd.setNodeTemperatureOption(JSONUtil.toJsonStr(CollUtil.newArrayList(5)));
        }
        // 数据信息补充
        objAdd.setCreateTime(DateUtil.currentSeconds());
        objAdd.setCreateBy(SecurityUtils.getSysUser().getUserId());
        // 执行添加数据信息
        Validator.validateTrue(this.save(objAdd), "common.save");
        return Boolean.TRUE;
    }

    /**
     * @Description: 后台管理修改
     * @Param: obj
     * @return: Boolean
     * @author cy code generator
     * @date 2021-07-09 17:40:26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editAdminObj(AdminDogHxDevNodeEditDto obj) {
        // 查询数据信息是否存在
        DogHxDevNode objInfo = this.getById(obj.getId());
        // 判断数据是否存
        Validator.validateNotNull(objInfo, "common.noData");
        // 判断设备 - 是否存在
        Validator.validateNotNull(hxDevMapper.selectById(obj.getDevId()), "common.noData");
        // 数据信息转换
        DogHxDevNode editObj = BeanUtil.toBean(obj, DogHxDevNode.class);
        // 判断水温信息
        if (Validator.equal(obj.getNodeType(), 0)) {
            // 洗衣机
            // 判断是否为空
            Validator.validateTrue(CollUtil.isNotEmpty(obj.getNodeTemperatureOptions()), "dev.node.data.noNull");
            // 判断如果大于三个提示
            Validator.validateTrue(CollUtil.size(obj.getNodeTemperatureOptions()) <= 3, "dev.node.data.max");
            // 去重
            List<Integer> distinct = CollUtil.distinct(obj.getNodeTemperatureOptions());
            // 判断数据
            for (int i = distinct.size() - 1; i >= 0; i--) {
                // 判断是否小于 0 大于3
                Validator.validateFalse(distinct.get(i) < 0, "dev.node.data.error");
                Validator.validateFalse(distinct.get(i) > 3, "dev.node.data.error");
            }
            editObj.setNodeTemperatureOption(JSONUtil.toJsonStr(distinct));
        } else {
            // 烘干机
            editObj.setNodeTemperatureOption(JSONUtil.toJsonStr(CollUtil.newArrayList(5)));
        }
        // 补充数据信息
        editObj.setUpdateTime(DateUtil.currentSeconds());
        editObj.setUpdateBy(SecurityUtils.getSysUser().getUserId());
        // 执行修改信息
        Validator.validateTrue(this.updateById(editObj), "operation failed");
        return Boolean.TRUE;
    }

    /**
     * @Description: 删除
     * @Param: ids
     * @return: Boolean
     * @author cy code generator
     * @date 2021-07-09 17:40:26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(Long id) {
        // 查询数据信息 - 根据标识
        DogHxDevNode objInfo = this.getById(id);
        // 判断数据信息是否
        Validator.validateNotNull(objInfo, "common.noData");
        // 判断是否有关联子集数据 - 订单数据信息
        Integer count = orderItmeMapper.selectCount(Wrappers.<DogHxOrderItme>lambdaQuery().eq(DogHxOrderItme::getDevNodeId, id));
        Validator.validateTrue(count <= 0, "dev.node.relation.data");
        // 执行删除数据信息
        Validator.validateTrue(this.removeById(id), "common.del");
        return Boolean.TRUE;
    }

    /**
     * @功能描述: 设备有几种挂载类型
     * @author He
     * @date 2021/7/12
     */
    @Override
    public Integer getApiDevBdType(ApiDogHxDevNodeInfoDto dto) {
        // 查询设备有几种类型
        List<Integer> apiDevBdType = this.baseMapper.getApiDevBdType(dto);
        // 判断小于2个类型时候 - 判断类型是什么
        Integer returnVal = Integer.valueOf(0);
        if (apiDevBdType.size() == 1) {
            if (apiDevBdType.get(0) == 0) {
                returnVal = Integer.valueOf(1);
            } else {
                returnVal = Integer.valueOf(2);
            }
        } else if (apiDevBdType.size() >= 2) {
            returnVal = Integer.valueOf(3);
        }
        return returnVal;
    }

    /**
     * @功能描述: 设备挂载大小类型
     * @author He
     * @date 2022/1/10
     */
    @Override
    public List<Integer> getApiDevBdTypeSize(ApiDogHxDevNodeInfoDto dto) {
        Validator.validateNotNull(dto, "Data cannot be empty");
        // 查询设备标识
        Validator.validateNotNull(dto.getDevId(), "Device ID cannot be empty");
        // 设备类型
        Validator.validateNotNull(dto.getNodeType(), "Device type cannot be empty");
        // 分组查询
        return this.baseMapper.getApiDevBdTypeSize(dto);
    }

    /**
     * @功能描述: 禁用设备节点
     * @author He
     * @date 2021/8/6
     */
    @Override
    public Boolean editApiDevNodeDisable(Long devNodeId) {
        // 查询设备节点是否存在
        DogHxDevNode devNode = this.getById(devNodeId);
        Validator.validateNotNull(devNode, "Node device does not exist");
        // 判断是什么做处理
        DogHxDevNode editHxDevNode = new DogHxDevNode();
        editHxDevNode.setId(devNodeId);
        editHxDevNode.setNodeDisable(Validator.equal(devNode.getNodeDisable(), 0) ? 1 : 0);
        Validator.validateTrue(editHxDevNode.updateById(), "operation failed");
        return Boolean.TRUE;
    }

}
